package jmars.mobile.worker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;

import jmars.mobile.common.Investigator;
import jmars.mobile.datainvestigation.Summary;

public class Distinct implements Investigator {

	@Override
	public Long compute(File[] files, FileWriter writer) throws IOException {
		Long total = 0L;
		Long line ;
		Long blank = 0L;
		FileInputStream fis;
		BufferedReader reader;
		String row;
		String[] cols;
		Set<String> distinct=new HashSet<String>();
		String groupKey;
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("0,000");
		for(File inputFile:files){
			fis = new FileInputStream(inputFile);
			reader = new BufferedReader(new InputStreamReader(fis));
			line=0L;
			while((row=reader.readLine())!=null){
				line++;
				cols=row.split(Summary.columnSeperator);
				if(Summary.filterRow(cols,true,false)) continue;
				groupKey="";
				for(Integer idx:Summary.groupColumnIdx){
					if(idx<cols.length) groupKey=groupKey+"$_$"+cols[idx]+"$_$";
					else{
						groupKey=groupKey+"$_$$_$";
					}
				}
				if(groupKey.replaceAll("\\$_\\$", "").replaceAll("\\s", "").equals(""))blank++;
				if(!distinct.contains(groupKey))distinct.add(groupKey);
				total++;
			}
			reader.close();
			fis.close();
			System.out.println(inputFile.getName()+" finished at "+total);
		}
		writer.write("%"+Summary.groupColumns+":"+df.format(distinct.size())+"\r\n");
		writer.write("%Null/blank:"+df.format(blank)+"\r\n");
		writer.write("%total:"+df.format(total)+"\r\n");
		if(distinct.size()<=1000){
			writer.write("%*****************details******************\r\n");
			for(String s:distinct){
				writer.write(s.replaceAll("\\$_\\$\\$_\\$", ",").replaceAll("\\$_\\$","")+"\r\n");
			}
		}
		return total;
	}

}
